1854B - Earn or Unlock - CodeForces Solution


bitmasks brute force dp

Please click on ads to support us..

C++ Code:

#include <iostream>
#include <cstring>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <algorithm>
#include <iomanip>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <bitset>

using namespace std;

#define pb push_back
#define x first
#define y second
#define ll long long

const int MOD = 1e9+7, INF = 1e9, NMAX = 2e5+5;
ll v[NMAX], sp[NMAX];
bitset<NMAX> dp;
void solve()
{
    ll n, i, j, ans = 0LL;
    cin >> n;
    for(i = 1; i <= n; i++)
    {
        cin >> v[i];
        sp[i] = sp[i-1] + v[i];
    }
    dp.set(1);
    dp.set(v[1] + 1);
    for(i = 2; i <= n; i++)
        dp |= (((dp >> i) << i) << v[i]);
    for(j = 0; j < NMAX; j++)
        if(dp[j])
            ans = max(ans, sp[min(n, j)] - j + 1);
    cout << ans << '\n';
}
int main()
{
    cin.tie(0)->sync_with_stdio(0);
    cin.exceptions(cin.failbit);
    int T;
    T = 1;
    while(T--)
        solve();
    return 0;
}


Comments

Submit
0 Comments
More Questions

938A - Word Correction
159C - String Manipulation 10
258A - Little Elephant and Bits
1536C - Diluc and Kaeya
1428C - ABBB
1557A - Ezzat and Two Subsequences
255A - Greg's Workout
1059A - Cashier
1389C - Good String
1561A - Simply Strange Sort
1337B - Kana and Dragon Quest game
137C - History
1443C - The Delivery Dilemma
6C - Alice Bob and Chocolate
1077C - Good Array
285B - Find Marble
6A - Triangle
1729A - Two Elevators
1729B - Decode String
1729C - Jumping on Tiles
1729E - Guess the Cycle Size
553B - Kyoya and Permutation
1729D - Friends and the Restaurant
1606C - Banknotes
580C - Kefa and Park
342A - Xenia and Divisors
1033A - King Escape
39D - Cubical Planet
1453A - Cancel the Trains
645A - Amity Assessment